package com.icow.wms.basic.service.impl;

import java.util.List;

import com.icow.wms.basic.domain.SsxFinish;
import com.icow.wms.basic.domain.TransportOrder;
import com.icow.wms.basic.utils.OpenTcs;
import com.icow.wms.basic.utils.SsxDockMoment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icow.wms.basic.mapper.SsxPeripheralMapper;
import com.icow.wms.basic.domain.SsxPeripheral;
import com.icow.wms.basic.service.ISsxPeripheralService;

/**
 * ssxService业务层处理
 *
 * @author bcs
 * @date 2024-08-08
 */
@Service
public class SsxPeripheralServiceImpl implements ISsxPeripheralService {
    @Autowired
    private SsxPeripheralMapper ssxPeripheralMapper;

    /**
     * 查询ssx
     *
     * @param id ssxID
     * @return ssx
     */
    @Override
    public SsxPeripheral selectSsxPeripheralById(Long id) {
        return ssxPeripheralMapper.selectSsxPeripheralById(id);
    }

    @Override
    public void ssxDockThreadBoot(TransportOrder transportOrder, String ssxDockMoment) {
        if (ssxDockMoment.equals(SsxDockMoment.PICK_UP_FINISH)) {
            String locationName = OpenTcs.getLoadLocation(transportOrder);
            SsxPeripheral ssxPeripheral = getPeripheral(locationName);
            if (ssxPeripheral != null) {
                SsxFinish ssxFinish = new SsxFinish(ssxPeripheral.getIp(), ssxPeripheral.getLoadFinish(),
                        ssxPeripheral.getLoadFinishAck());
                new Thread(ssxFinish).start();
            }
        } else if (ssxDockMoment.equals(SsxDockMoment.PUT_DOWN_FINISH)) {
            String locationName = OpenTcs.getUnloadLocation(transportOrder);
            SsxPeripheral ssxPeripheral = getPeripheral(locationName);
            if (ssxPeripheral != null) {
                SsxFinish ssxFinish = new SsxFinish(ssxPeripheral.getIp(), ssxPeripheral.getUnloadFinish(),
                        ssxPeripheral.getUnloadFinishAck());
                new Thread(ssxFinish).start();
            }
        }

    }

    /**
     * 查询ssx列表
     *
     * @param ssxPeripheral ssx
     * @return ssx
     */
    @Override
    public List<SsxPeripheral> selectSsxPeripheralList(SsxPeripheral ssxPeripheral) {
        return ssxPeripheralMapper.selectSsxPeripheralList(ssxPeripheral);
    }

    /**
     * 新增ssx
     *
     * @param ssxPeripheral ssx
     * @return 结果
     */
    @Override
    public int insertSsxPeripheral(SsxPeripheral ssxPeripheral) {
        return ssxPeripheralMapper.insertSsxPeripheral(ssxPeripheral);
    }

    /**
     * 修改ssx
     *
     * @param ssxPeripheral ssx
     * @return 结果
     */
    @Override
    public int updateSsxPeripheral(SsxPeripheral ssxPeripheral) {
        return ssxPeripheralMapper.updateSsxPeripheral(ssxPeripheral);
    }

    /**
     * 批量删除ssx
     *
     * @param ids 需要删除的ssxID
     * @return 结果
     */
    @Override
    public int deleteSsxPeripheralByIds(Long[] ids) {
        return ssxPeripheralMapper.deleteSsxPeripheralByIds(ids);
    }

    /**
     * 删除ssx信息
     *
     * @param id ssxID
     * @return 结果
     */
    @Override
    public int deleteSsxPeripheralById(Long id) {
        return ssxPeripheralMapper.deleteSsxPeripheralById(id);
    }

    @Override
    public SsxPeripheral getPeripheral(String locationName) {
        SsxPeripheral ssxPeripheral = new SsxPeripheral();
        ssxPeripheral.setWorkLocation(locationName);
        List<SsxPeripheral> ssxPeripherals = ssxPeripheralMapper.selectSsxPeripheralList(ssxPeripheral);
        if (ssxPeripherals.size() != 1) {
            return null;
        }
        return ssxPeripherals.get(0);
    }
}
